#!/usr/bin/env python3
# encoding: utf-8


def help():
    import logging
    logging.basicConfig(
        level=logging.INFO,
        format='%(levelname)s:%(asctime)s:%(message)s')
    s = "copyToRemote\ntoHdfs\nBash\n"
    logging.info(s)
    return s


def copyToRemote(path, data):
    import logging
    logging.basicConfig(
        level=logging.INFO,
        format='%(levelname)s:%(asctime)s:%(message)s')
    try:
        with open(path, "wb") as fp:
            fp.write(data)
    except Exception as e:
        return False
    return path


def Bash(cmd):
    import subprocess
    import logging
    from os import environ
    from config import env
    _env = environ.copy()
    for n in env.__dir__():
        if not n.startswith("_"):
            if n not in _env:
                _env[n] = getattr(env, n)
            else:
                _env[n] += getattr(env, n)
    
    logging.basicConfig(
        level=logging.INFO,
        format='%(levelname)s:%(asctime)s:%(message)s')
    pro = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE ,stderr=subprocess.PIPE, env=_env)
    stat = pro.wait()
    info = pro.stdout.read().decode() if stat == 0 else pro.stderr.read().decode()
    logging.info(info)
    return stat, info

